package com.zyj.myapplication.Activity;

import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zyj.myapplication.Model.ActivityModel;
import com.zyj.myapplication.Model.TrainModel;
import com.zyj.myapplication.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BaoMingActivity extends AppCompatActivity {
    private Button act_buy;
    private TextView pay;
    private EditText participantname,participanttel,adultnum,studentnum,paynotice;
    private Double allprice,stuprice,aduprice,num_adultnum,num_studentnum;
    private int user_id;
    private String act_id,str_name,str_tel,str_adultnum,str_studentnum,str_allpay,str_notice,adunum,stunum;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bao_ming);
        init();
        event();
    }

    private void event() {
        act_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buy();
            }
        });
    }

    private void init() {
        act_buy=(Button)findViewById(R.id.activity_buy);
        participantname= (EditText) findViewById(R.id.participantname);
        participanttel= (EditText) findViewById(R.id.participanttel);
        adultnum= (EditText) findViewById(R.id.adultnum);
        paynotice = (EditText) findViewById(R.id.paynotice);
        studentnum= (EditText) findViewById(R.id.studentnum);
        pay= (TextView) findViewById(R.id.pay);

        user_id = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id",0);
        Intent intent = getIntent();
        act_id= intent.getStringExtra("act_id");
        aduprice = Double.valueOf(intent.getStringExtra("aduprice"));
        stuprice = Double.valueOf(intent.getStringExtra("stuprice"));
        adunum = adultnum.getText().toString();
        stunum = studentnum.getText().toString();
        num_adultnum = Double.parseDouble(adunum);
        num_studentnum= Double.parseDouble(stunum);
        allprice = num_adultnum*aduprice+num_studentnum*stuprice;
        pay.setText(allprice.toString()+"元");
    }
    private void buy(){
        ActivityModel activityModel = new ActivityModel();
        str_name = participantname.getText().toString();
        str_tel = participanttel.getText().toString();
        str_adultnum= adultnum.getText().toString();
        str_studentnum = studentnum.getText().toString();
        str_notice= paynotice.getText().toString();
        str_allpay = allprice.toString();
        Call<Integer> call = activityModel.signAct(str_name,act_id,str_tel,str_adultnum,str_studentnum,"1",str_allpay,str_notice,user_id);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()==0){
                    Toast.makeText(BaoMingActivity.this, "下单失败", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(BaoMingActivity.this,"下单成功",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }
}


